package com.serializetool.handler;

import com.serializetool.Request;
import com.serializetool.Response;
import com.serializetool.SerializingUtil;
import com.serializetool.Validation;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
        Response response = (Response) msg;
        response.setMagicNum(Validation.RES_MES.getCode());
        byte[] bytes = SerializingUtil.serialize(response);
        System.out.println(response);
        out.writeInt(bytes.length);
        out.writeBytes(bytes);
    }

}
